package me.firstapp.bbs.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import me.firstapp.bbs.base.BaseController;
import me.firstapp.common.json.PageObject;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.module.bbs.JsonBbsNotification;
import me.firstapp.common.module.bbs.JsonShopNotification;
import me.firstapp.common.module.bbs.JsonSysNotification;
import me.firstapp.common.utils.ResponseUtils;
import me.firstapp.spi.bbs.NotificationService;

@Controller
@RequestMapping(value = "/notification")
public class NotificationController extends BaseController {

	@Autowired
	private NotificationService notificationService;

	@RequestMapping(value = "/bbsNotification", method = RequestMethod.GET)
	public ModelAndView bbsNotification(
			@RequestParam(value = "pageNo", required = true, defaultValue = "1") Integer pageNo,
			@RequestParam(value = "pageSize", required = true, defaultValue = "20") Integer pageSize,
			HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		Long memberId = getCurrentLoginMemberId(request, response);
		PageObject<JsonBbsNotification> result = notificationService.findMemberBbsNotificationPage(pageNo, 500,
				memberId, null);
		modelMap.addAttribute("notificationPage", result.getPage());
		modelMap.addAttribute("tab", "bbs");
		return new ModelAndView("notification/notification", modelMap);
	}

	@RequestMapping(value = "/shopNotification", method = RequestMethod.GET)
	public ModelAndView shopNotification(
			@RequestParam(value = "pageNo", required = true, defaultValue = "1") Integer pageNo,
			@RequestParam(value = "pageSize", required = true, defaultValue = "20") Integer pageSize,
			HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		Long memberId = getCurrentLoginMemberId(request, response);
		PageObject<JsonShopNotification> result = notificationService.findMemberShopNotificationPage(pageNo, 500,
				memberId, null);
		modelMap.addAttribute("notificationPage", result.getPage());
		modelMap.addAttribute("tab", "shop");
		return new ModelAndView("notification/notification", modelMap);
	}

	@RequestMapping(value = "/systemNotification", method = RequestMethod.GET)
	public ModelAndView systemNotification(
			@RequestParam(value = "pageNo", required = true, defaultValue = "1") Integer pageNo,
			@RequestParam(value = "pageSize", required = true, defaultValue = "20") Integer pageSize,
			HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		Long memberId = getCurrentLoginMemberId(request, response);
		PageObject<JsonSysNotification> result = notificationService.findMemberSysNotificationPage(pageNo, 500,
				memberId, null);
		modelMap.addAttribute("notificationPage", result.getPage());
		modelMap.addAttribute("tab", "system");
		return new ModelAndView("notification/notification", modelMap);
	}

	@RequestMapping(value = "/setNotificationReaded.do", method = RequestMethod.POST)
	public void setNotificationReaded(String type, HttpServletRequest request, HttpServletResponse response) {
		Long memberId = getCurrentLoginMemberId(request, response);
		SingleObject<Object> result = notificationService.setNotificationReaded(memberId, type);
		ResponseUtils.renderJson(response, result);
	}

}
